// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

import { CupertinoPalette, CupertinoFontSettings, Icons } from "styling.slint";
import { FocusBorder } from "components.slint";
import { SpinBoxBase } from "../common/spinbox-base.slint";

component SpinBoxButton {
    in property <bool> enabled <=> touch-area.enabled;
    in property <image> icon <=> icon.source;

    callback clicked <=> touch-area.clicked;

    private property <brush> background: CupertinoPalette.accent-background;
    private property <brush> icon-color: CupertinoPalette.accent-foreground;

    min-width: 16px;
    horizontal-stretch: 0;

    states [
        disabled when !touch-area.enabled: {
            opacity: 0.5;
            icon-color: CupertinoPalette.foreground-secondary;
        }
        pressed when touch-area.pressed: {
            root.background: CupertinoPalette.secondary-accent-background;
        }
    ]

    Rectangle {
        y: (parent.height - self.height) / 2;
        width: 14px;
        height: self.width;

        animate background { duration: 150ms; }

        if (root.enabled): Rectangle {
            width: 100%;
            height: 100%;
            border-radius: 4px;
            background: root.background;

            Rectangle {
                border-radius: parent.border-radius;
                background: CupertinoPalette.dimmer;
                opacity: 0.17;
            }
        }

        icon := Image {
            image-fit: contain;
            colorize: root.icon-color;
            width: 12px;
            accessible-role: none;

            animate colorize { duration: 150ms; }
        }
    }

    touch-area := TouchArea { }
}

export component SpinBox {
    in property <int> minimum <=> base.minimum;
    in property <int> maximum <=> base.maximum;
    in property <bool> enabled <=> base.enabled;
    in property <int> step-size <=> base.step-size;
    in property <TextHorizontalAlignment> horizontal-alignment <=> base.horizontal-alignment;
    out property <bool> has-focus <=> base.has-focus;
    in-out property <int> value <=> base.value;

    callback edited <=> base.edited;

    private property <brush> background: CupertinoPalette.control-background;

    min-width: max(128px, layout.min-width);
    min-height: max(22px, layout.min-height);
    vertical-stretch: 0;
    horizontal-stretch: 1;
    forward-focus: base;

    accessible-role: spinbox;
    accessible-enabled: root.enabled;
    accessible-value: root.value;
    accessible-value-minimum: root.minimum;
    accessible-value-maximum: root.maximum;
    accessible-value-step: (root.maximum - root.minimum) / 100;
    accessible-action-set-value(v) => {
        if v.is-float() {
            base.update-value(v.to-float());
        }
    }
    accessible-action-increment => {
        base.increment();
    }
    accessible-action-decrement => {
        base.decrement();
    }

    states [
        disabled when !root.enabled: {
            base.color: CupertinoPalette.foreground;
            root.background: CupertinoPalette.tertiary-control-background;
        }
    ]

    FocusBorder {
        x: (parent.width - self.width) / 2;
        y: (parent.height - self.height) / 2;
        width: parent.width + 6px;
        height: parent.height + 6px;
        border-radius: 8px;
        has-focus: root.has-focus;
    }

    Rectangle {
        border-radius: 5px;
        background: root.background;

        Rectangle {
            border-radius: parent.border-radius;
            background: root.background;
            border-width: 1px;
            border-color: CupertinoPalette.border;
            opacity: root.enabled ? 1 : 0.5;
        }
    }

    layout := HorizontalLayout {
        padding-left: 7px;
        padding-right: 2px;
        spacing: 2px;

        Rectangle {
            clip: true;
            horizontal-stretch: 1;

            base := SpinBoxBase {
                opacity: root.enabled ? 1 : 0.5;
                width: 100%;
                color: CupertinoPalette.foreground;
                font-size: CupertinoFontSettings.body.font-size;
                font-weight: CupertinoFontSettings.body.font-weight;
                selection-background-color: CupertinoPalette.selection-background;
                selection-foreground-color: self.color;
            }
        }

        SpinBoxButton {
            visible: root.enabled;
            icon: Icons.chevron-up;
            enabled: root.enabled;

            clicked => {
                base.increment();
            }
        }

        SpinBoxButton {
            visible: root.enabled;
            icon: Icons.chevron-down;
            enabled: root.enabled;

            clicked => {
                base.decrement();
            }
        }
    }
}
